M = {}

local targetScale
local counter
local active
local cameraX
local cameraY

local newCamera = function(game,player)
    
    --scale = 0.2
    scale = 1
    game:scale(scale,scale)
    targetScale = 1
    counter = 100
    active = false
    --posicion inicial de la camara
    game.x = display.contentWidth/3 - (player:getX() * scale)
    game.y = display.contentHeight/2 - (player:getY() * scale) 
    --posicion player
    cameraX = player:getX()
    cameraY = player:getY()
    
    local zoomInB = display.newRect(display.contentWidth - 60, display.contentHeight - 200, 50, 50 )
    zoomInB.alpha = 0.2
    function doTouchZoomInB(self, event)
        
        if event.phase == "began" then
            targetScale = scale* 1.15
            return true
        end
    end
    
    zoomInB.touch = doTouchZoomInB
    zoomInB:addEventListener("touch", zoomInB)
    
    local zoomOutB = display.newRect(display.contentWidth - 60, display.contentHeight - 100, 50, 50 )
    zoomOutB.alpha = 0.2
    function doTouchZoomOutB(self, event)
        
        if event.phase == "began" then
            if(scale > 0.6)then
                targetScale = scale* 0.85
            end
            return true
        end
    end
    
    zoomOutB.touch = doTouchZoomOutB
    zoomOutB:addEventListener("touch", zoomOutB)
end

M.newCamera = newCamera

local function checkCamera(game, player,trans,factor)
    if( game.x + cameraX -  display.contentWidth/3 < -(factor*10)) then
        trans.x =factor
    elseif( game.x + cameraX - display.contentWidth/3 >factor*10) then
        trans.x =-factor
    end
    if( game.y + cameraY - display.contentHeight/2 <-(factor*10)) then
        trans.y = factor
    elseif(game.y + cameraY - display.contentHeight/2 >factor*10) then
        trans.y = -factor
    end
end

-- Camera follows player automatically
local moveCamera= function(game,background,player)
    if(not active)then
        counter = counter - 1
        if(counter == 0)then
            active = true
        end
        return nil
    end
    
    if(targetScale ~= nil and ( math.abs( targetScale -scale ) > 0.03 ))then
        --print("scale")
        if(targetScale > scale)then
            scale = scale* 1.005
            game:scale(1.005,1.005)
        end
        if(targetScale < scale)then
            scale = scale* 0.995
            game:scale(0.995,0.995)
        end
        
    end
    
    if (player.planet == nil or player.pVector ~= nil) then
        cameraX = player:getX() * scale
        cameraY = player:getY() * scale
        
    else
        cameraX = player.planet:getX() * scale
        cameraY = player.planet:getY() * scale
    end
    
    local trans ={}
    trans.x=0
    trans.y=0
    
    for i = 2, 6 do
        checkCamera(game, player,trans,i)
    end
    
    if(trans.x ~=0 or trans.y~=0)then
        --print("trans:"..trans.x..","..trans.y)
        --game:translate( trans.x, trans.y )
        game.x,game.y = game.x + trans.x, game.y + trans.y
        background.x,background.y = background.x + trans.x/5, background.y + trans.y/5
    end
end

M.moveCamera= moveCamera

return M